import sys
import PySide6.QtWidgets as QtWidgets
from PySide6.QtCore import Qt

def set_button_style(button, default_color="white", pressed_color="darkGray", text_color="black",
                     pressed_text_color="white"):
    button.setStyleSheet(f"""
        QPushButton {{
            background-color: {default_color};
            color: {text_color};
        }}
        QPushButton:pressed {{
            background-color: {pressed_color};
            color: {pressed_text_color};
        }}
    """)

class PathSelectionWindow(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("选择路径")
        self.setMinimumSize(400, 200)

        layout = QtWidgets.QVBoxLayout()

        # 路径显示部分
        path_layout = QtWidgets.QHBoxLayout()
        self.path_label = QtWidgets.QLabel("路径:")
        self.path_display = QtWidgets.QLineEdit()
        self.path_display.setReadOnly(True)
        browse_button = QtWidgets.QPushButton("浏览")
        browse_button.clicked.connect(self.browse_path)

        path_layout.addWidget(self.path_label)
        path_layout.addWidget(self.path_display)
        path_layout.addWidget(browse_button)

        # 确定按钮
        confirm_button = QtWidgets.QPushButton("确定")
        confirm_button.clicked.connect(self.confirm_and_open_main)

        layout.addLayout(path_layout)
        layout.addWidget(confirm_button)

        self.setLayout(layout)

    def browse_path(self):
        """打开路径选择框"""
        directory = QtWidgets.QFileDialog.getExistingDirectory(self, "选择文件夹路径")
        if directory:
            self.path_display.setText(directory)

    def confirm_and_open_main(self):
        """进入主界面"""
        if not self.path_display.text():
            QtWidgets.QMessageBox.warning(self, "警告", "请选择路径后再确认！")
            return

        # 打开主窗口
        self.main_window = Main_Window(self.path_display.text())
        self.main_window.show()
        self.close()  # 关闭当前路径选择窗口

class FirstSubWindow(QtWidgets.QWidget):
    def __init__(self, title):
        super().__init__()

        self.setWindowTitle(title)

        main_layout = QtWidgets.QVBoxLayout()

        top_layout = self.create_top_layout()

        image_layout = self.create_image_layout()

        main_layout.addLayout(top_layout)
        main_layout.addLayout(image_layout)

        self.setLayout(main_layout)

    def create_top_layout(self):
        """创建第一行的布局，包括按钮、文本显示和数值选择框"""
        top_layout = QtWidgets.QHBoxLayout()

        button1 = QtWidgets.QPushButton("原始信号")
        set_button_style(button1)
        button2 = QtWidgets.QPushButton("放电率检测")
        set_button_style(button2)


        label_1 = QtWidgets.QLabel("选中通道页")
        label_2 = QtWidgets.QLabel("选针")

        spinbox_1 = QtWidgets.QSpinBox()
        spinbox_1.setRange(1, 4)
        spinbox_1.setValue(1)
        spinbox_1.setFixedWidth(60)

        spinbox_2 = QtWidgets.QSpinBox()
        spinbox_2.setRange(1, 64)
        spinbox_2.setValue(1)
        spinbox_2.setFixedWidth(70)

        top_layout.addWidget(button1)
        top_layout.addWidget(button2)
        top_layout.addWidget(label_1)
        top_layout.addWidget(spinbox_1)
        top_layout.addWidget(label_2)
        top_layout.addWidget(spinbox_2)

        top_layout.setAlignment(Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft)

        # 组件赋值
        # 连接按钮的点击事件
        self.set_button_selected(button1, True)
        self.set_button_selected(button2, False)
        button1.clicked.connect(lambda: self.on_button_clicked(button1, button2))
        button2.clicked.connect(lambda: self.on_button_clicked(button2, button1))


        return top_layout

    def create_image_layout(self):
        """创建第二行的布局，用于显示图像"""
        image_layout = QtWidgets.QVBoxLayout()

        image_view = QtWidgets.QGraphicsView()
        image_scene = QtWidgets.QGraphicsScene()
        image_view.setScene(image_scene)
        image_view.setStyleSheet("border: 1px solid black;")
        image_scene.update()

        image_layout.addWidget(image_view)

        image_view.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)

        return image_layout

    def on_button_clicked(self, clicked_button, other_button):
        """按钮点击事件处理：切换选中状态"""
        # 切换当前按钮的状态
        self.set_button_selected(clicked_button, True)
        # 取消另一个按钮的选中状态
        self.set_button_selected(other_button, False)

    def set_button_selected(self, button, is_selected):
        """设置按钮的选中状态并改变样式"""
        if is_selected:
            button.setStyleSheet("background-color: darkGray;")
        else:
            button.setStyleSheet("background-color: none;")

class SecondSubWindow(QtWidgets.QWidget):
    def __init__(self, title):
        super().__init__()

        self.setWindowTitle(title)

        main_layout = QtWidgets.QVBoxLayout()

        top_layout = self.create_top_layout()

        image_layout = self.create_image_layout()

        main_layout.addLayout(top_layout)
        main_layout.addLayout(image_layout)

        self.setLayout(main_layout)

    def create_top_layout(self):
        """创建第一行的布局，包括按钮、文本显示和数值选择框"""
        top_layout = QtWidgets.QHBoxLayout()

        button = QtWidgets.QPushButton("刷新")
        set_button_style(button)

        label1 = QtWidgets.QLabel("阈值")
        label2 = QtWidgets.QLabel("uv")

        spinbox = QtWidgets.QSpinBox()
        spinbox.setRange(-100, 60)
        spinbox.setValue(-63)
        spinbox.setFixedWidth(80)

        top_layout.addWidget(button)
        top_layout.addWidget(label1)
        top_layout.addWidget(spinbox)
        top_layout.addWidget(label2)

        top_layout.setAlignment(Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft)

        return top_layout

    def create_image_layout(self):
        """创建第二行的布局，用于显示图像"""
        image_layout = QtWidgets.QVBoxLayout()

        image_view = QtWidgets.QGraphicsView()
        image_scene = QtWidgets.QGraphicsScene()
        image_view.setScene(image_scene)
        image_view.setStyleSheet("border: 1px solid black;")
        image_scene.update()

        image_layout.addWidget(image_view)

        image_view.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)

        return image_layout

class ThirdSubWindow(QtWidgets.QWidget):
    def __init__(self, title):
        super().__init__()

        self.setWindowTitle(title)
        self.fft_window = None
        main_layout = QtWidgets.QVBoxLayout()

        top_layout = self.create_top_layout()

        image_layout = self.create_image_layout()

        main_layout.addLayout(top_layout)
        main_layout.addLayout(image_layout)

        self.setLayout(main_layout)

    def create_top_layout(self):
        """创建第一行的布局，包括按钮、文本显示和数值选择框"""
        top_layout = QtWidgets.QHBoxLayout()

        trap_button = QtWidgets.QRadioButton("50Hz陷波")
        button = QtWidgets.QPushButton("时频图")
        set_button_style(button)

        label_filiter = QtWidgets.QLabel("滤波")
        combo_box_filiter = QtWidgets.QComboBox()
        combo_box_filiter.addItem("None")
        combo_box_filiter.addItem("HP 250Hz")
        combo_box_filiter.addItem("HP 750Hz")
        combo_box_filiter.addItem("LP 250Hz")
        combo_box_filiter.addItem("LP 500Hz")
        combo_box_filiter.addItem("BP 10Hz~250Hz")
        combo_box_filiter.addItem("BP 250Hz~5kHz")
        # combo_box_filiter.addItem("Trapped 50Hz")

        top_layout.addWidget(trap_button)
        top_layout.addSpacing(5)
        top_layout.addWidget(label_filiter)
        top_layout.addWidget(combo_box_filiter)
        top_layout.addWidget(button)

        top_layout.setAlignment(Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft)

        # 组件赋值
        button.clicked.connect(self.open_new_window)


        return top_layout

    def create_image_layout(self):
        """创建第二行的布局，用于显示图像"""
        image_layout = QtWidgets.QVBoxLayout()

        image_view = QtWidgets.QGraphicsView()
        image_scene = QtWidgets.QGraphicsScene()
        image_view.setScene(image_scene)
        image_view.setStyleSheet("border: 1px solid black;")
        image_scene.update()

        image_layout.addWidget(image_view)

        image_view.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)

        return image_layout

    def open_new_window(self):
        if self.fft_window is None:
            self.fft_window = FFT_Window()
            self.fft_window.show()
        else:
            if self.fft_window.isVisible():  # 如果窗口已显示，则隐藏
                self.fft_window.hide()
            else:
                self.fft_window.show()  # 如果窗口已隐藏，则显示

class FourthSubWindow(QtWidgets.QWidget):
    def __init__(self, title):
        super().__init__()

        self.setWindowTitle(title)

        main_layout = QtWidgets.QVBoxLayout()

        top_layout = self.create_top_layout()

        bottom_layout = QtWidgets.QHBoxLayout()

        image1_layout = self.create_image_layout()
        image2_layout = self.create_image_layout()
        bottom_layout.addLayout(image1_layout)
        bottom_layout.addLayout(image2_layout)

        bottom_layout.setStretch(0, 1)
        bottom_layout.setStretch(1, 3)

        main_layout.addLayout(top_layout)
        main_layout.addLayout(bottom_layout)

        self.setLayout(main_layout)

    def create_top_layout(self):
        """创建第一行的布局，包括按钮、文本显示和数值选择框"""
        top_layout = QtWidgets.QHBoxLayout()

        button = QtWidgets.QPushButton("刷新")
        set_button_style(button)

        label1 = QtWidgets.QLabel("阈值")
        label2 = QtWidgets.QLabel("uv")

        spinbox = QtWidgets.QSpinBox()
        spinbox.setRange(-100, 60)
        spinbox.setValue(-63)
        spinbox.setFixedWidth(80)

        top_layout.addWidget(button)
        top_layout.addWidget(label1)
        top_layout.addWidget(spinbox)
        top_layout.addWidget(label2)

        top_layout.setAlignment(Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft)

        return top_layout

    def create_image_layout(self):
        """创建第二行的布局，用于显示图像"""
        image_layout = QtWidgets.QVBoxLayout()

        image_view = QtWidgets.QGraphicsView()
        image_scene = QtWidgets.QGraphicsScene()
        image_view.setScene(image_scene)
        image_view.setStyleSheet("border: 1px solid black;")
        image_scene.update()

        image_layout.addWidget(image_view)

        image_view.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)

        return image_layout

# 时频图界面
class FFT_Window(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("时频信号")
        self.setMinimumSize(800, 600)

        main_layout = QtWidgets.QVBoxLayout()

        top_layout = self.create_top_layout()
        image_layout = self.create_image_layout()

        main_layout.addLayout(top_layout)
        main_layout.addLayout(image_layout)

        self.setLayout(main_layout)

    def create_top_layout(self):
        """创建第一行的布局，包括按钮、文本显示和数值选择框"""
        top_layout = QtWidgets.QHBoxLayout()

        button_1 = QtWidgets.QPushButton("时域")
        button_2 = QtWidgets.QPushButton("频域")
        button_3 = QtWidgets.QPushButton("时频图")
        set_button_style(button_1)
        set_button_style(button_2)
        set_button_style(button_3)

        top_layout.addWidget(button_1)
        top_layout.addWidget(button_2)
        top_layout.addWidget(button_3)

        top_layout.setAlignment(Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft)

        self.set_button_selected(button_1, True)
        self.set_button_selected(button_2, False)
        self.set_button_selected(button_3, False)
        button_1.clicked.connect(lambda: self.on_button_clicked(button_1, button_2, button_3))
        button_2.clicked.connect(lambda: self.on_button_clicked(button_2, button_1, button_3))
        button_3.clicked.connect(lambda: self.on_button_clicked(button_3, button_1, button_2))

        return top_layout

    def create_image_layout(self):
        """创建第二行的布局，用于显示图像"""
        image_layout = QtWidgets.QVBoxLayout()

        image_view = QtWidgets.QGraphicsView()
        image_scene = QtWidgets.QGraphicsScene()
        image_view.setScene(image_scene)
        image_view.setStyleSheet("border: 1px solid black;")
        image_scene.update()

        image_layout.addWidget(image_view)

        image_view.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)

        return image_layout

    def on_button_clicked(self, clicked_button, other_button_1, other_button_2):
        """按钮点击事件处理：切换选中状态"""
        # 切换当前按钮的状态
        self.set_button_selected(clicked_button, True)
        # 取消另一个按钮的选中状态
        self.set_button_selected(other_button_1, False)
        self.set_button_selected(other_button_2, False)

    def set_button_selected(self, button, is_selected):
        """设置按钮的选中状态并改变样式"""
        if is_selected:
            button.setStyleSheet("background-color: darkGray;")
        else:
            button.setStyleSheet("background-color: none;")

class Main_Window(QtWidgets.QWidget):
    def __init__(self, path):
        super().__init__()

        self.setWindowTitle(path)

        self.setMinimumSize(1200, 900)

        grid_layout = QtWidgets.QGridLayout()

        self.module1 = FirstSubWindow("多通道信号检测")
        self.module2 = SecondSubWindow("多通道Spike信号检测")
        self.module3 = ThirdSubWindow("单通道信号检测")
        self.module4 = FourthSubWindow("单通道Spike检测")

        grid_layout.addWidget(self.module1, 0, 0)
        grid_layout.addWidget(self.module2, 0, 1)
        grid_layout.addWidget(self.module3, 1, 0)
        grid_layout.addWidget(self.module4, 1, 1)

        grid_layout.setRowStretch(0, 2)
        grid_layout.setRowStretch(1, 1)
        grid_layout.setColumnStretch(0, 1)
        grid_layout.setColumnStretch(1, 1)

        self.setLayout(grid_layout)

    def create_groupbox(self, title, text):
        """创建一个带有标签和按钮的GroupBox"""
        groupbox = QtWidgets.QGroupBox(title)
        layout = QtWidgets.QVBoxLayout()

        label = QtWidgets.QLabel(text)
        button = QtWidgets.QPushButton("Click Me")
        set_button_style(button)

        layout.addWidget(label)
        layout.addWidget(button)

        groupbox.setLayout(layout)
        return groupbox


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)

    # 显示路径选择界面
    path_window = PathSelectionWindow()
    path_window.show()

    sys.exit(app.exec())
